package name.bingo.bing.pay.service.message.impl;/**
 * create by yanbing on 2018/11/10 15:00
 */

import com.alibaba.dubbo.config.annotation.Service;
import name.bingo.bing.pay.service.message.api.MessageService;
import name.bingo.bing.pay.service.message.dao.MessageDao;
import name.bingo.bing.pay.service.message.entity.TractionMessage;
import name.bingo.bing.pay.service.message.mqservice.Producer;
import name.bingo.bing.pay.utils.IdGen;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 *
 * @param:
 * @return:
 * @auther: yanbing
 * @date: 2018/11/10 15:00
 **/
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private Producer producer;
    @Autowired
    private MessageDao messageDao;

    @Override
    public String justSave(TractionMessage message) {
        try{
            message.setId(message.getMessageId());
            message.setStatus("01");//待发送
            return saveMessage(message);//待发送
        }catch (Exception e){
            return "error";
        }

    }

    @Override
    public String confirmSend(TractionMessage message) {
        message.setId(message.getMessageId());
        message.setStatus("02");//待确认
        int confirmResult=messageDao.confirmMessage(message);
        if(confirmResult>0){//在接受消息处，确认掉消息
            producer.sendMessage(new ActiveMQQueue(message.getQueue()),message.getMessagebody());
        }else{
            return "no exist";
        }
        return "ok";
    }

    @Override
    public String canceSend(TractionMessage message) {
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String saveAndSend(TractionMessage message) {
        try{
            //save--待发送
            message.setId(message.getMessageId());
            message.setStatus("02");//直接待确认
            saveMessage(message);
            //send
            producer.sendMessage(new ActiveMQQueue(message.getQueue()),message.getMessagebody());
            //update--已发送
        }catch (Exception e){
            System.out.println(""+e.getMessage());
            return "error";
        }
        return "ok";
    }

    @Override
    public String directSend(TractionMessage message) {
        return null;
    }

    public String saveMessage(TractionMessage message){
        String result="ok";
        Map<String,Object> messageMap=messageDao.findById(message.getId());
        //
        if(messageMap==null || messageMap.size()==0){
            message.setAreadly_dead(false);
            message.setMessage_data_type("mapJson");
            message.setMessage_send_times(1);
            messageDao.insert(message);
        }else{
            result="exist";
            /*int message_send_times=(int)messageMap.get("message_send_times");
            message.setMessage_send_times(message_send_times++);
            message.setAreadly_dead(false);
            if(message_send_times>6){//5次死亡
                message.setAreadly_dead(true);
            }
            messageDao.update(message);*/
        }
        return result;
    }
}
